package Function;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import VWorldGameStudio.FCAction;
import VWorldGameStudio.FCEvent;
import com.google.gson.Gson;
import utils.FCDataSingles;

/**
 * 行为动作库
 * 
 * @author jungle
 *
 */
public class FCActionLibrary {
	private Vector<FCAction> actionList = new Vector<FCAction>();
	public Vector<String> actionName = new Vector<String>();
	private static final String ACTION_LIST_PATH = "./data/actionlist.vw";
	private static final String ACTIONS_PATH = "./data/action";
	private int actionallnum=0;
	private HashMap<String, FCAction> npcActions = new HashMap<>();
	/**
	 * 初始化动作类，读取数量
	 * 建议游戏启动方使用
	 * @date 2021-11-30
	 */
	public FCActionLibrary() {
		try {
			this.actionallnum=new File(ACTIONS_PATH).listFiles().length;
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 动作数据加载函数
	 * @date 2021-11-30
	 */ 
	public void loadActionData() {
		try {
			File[] f=new File(ACTIONS_PATH).listFiles();
			for(int i=0;i<actionallnum;i++) {
				FCAction action = FCDataSingles.getReader().readActionData(f[i].getPath());
				actionList.add(action);
				actionName.add(action.getName());
				Gson gson = new Gson();
				System.out.println("FCAction "+gson.toJson(action));
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取动作实际数据量
	 * @return integer
	 * @date 2021-11-30
	 */
	public int getActionAllNum() {
		return this.actionallnum;
	}


	/**
	 * 注册
	 * 
	 * @param isNew true：新增；false：编辑。
	 * @param name
	 * @param type
	 * @param face
	 * @param route
	 */
	public void registerAction(boolean isNew, String name, String type, int face, ArrayList<String> route) {
		if (isNew) {
			actionName.add(name);
			FCAction action = new FCAction(name, type, face, route);
			action.registerAction(name, type, face, route);
			actionList.add(action);
			FCDataSingles.getWriter().writeActionData(action);
		} else {
			actionList.get(actionName.indexOf(name)).registerAction(name, type, face, route);
			FCDataSingles.getWriter().writeActionData(actionList.get(actionName.indexOf(name)));
		}
	}

	public int getActionNum() {
		return actionList.size();
	}

	public String getActionNameOfID(int num) {
		return actionName.get(num);
	}

	public boolean isNameRepeat(String name) {
		if (actionName.indexOf(name) > -1)
			return true;
		else
			return false;
	}

	public FCAction getActionOfName(String name) {
		return actionList.get(actionName.indexOf(name));
	}

	public FCAction getActionOfNpcInfo(String key) {
		FCAction ret = npcActions.get(key);
		if(ret == null){
			String actinoName = key.split(",")[0];
			ret = getActionOfName(actinoName);
			if(ret.getType().equals(FCAction.TYPE_RANDOM)||ret.getType().equals(FCAction.TYPE_RANDOM_DIR)) {
				ret = ret.getClone();
				npcActions.put(key, ret);
			}
		}
		return ret;
	}

	public String getActionInfoOfNum(int num) {
		return actionList.get(num).getType();
	}
}
